/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.guide.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.Result;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.guide.entity.MobileEvaluate;
import com.jeeplus.modules.guide.entity.MobileEvaluateHistory;
import com.jeeplus.modules.guide.entity.MobileEvaluateSms;
import com.jeeplus.modules.guide.entity.MobileEvaluateValue;
import com.jeeplus.modules.guide.service.*;
import com.jeeplus.modules.guide.task.Send;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务Controller
 * @author azj
 * @version 2021-5-15
 */
@RestController
@RequestMapping("/mobileBusiness")
@RequiredArgsConstructor
public class MobileBusinessController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(MobileBusinessController.class);
	private final MobileBusinessService mobilebusinessService;
	private final MobileMatterService mobileMatterService;
	private final MobileEvaluateService mobileEvaluateService;
	private final MobileOtherService mobileOtherService;
	private final MobileSilentService mobileSilentService;
	private final Send send;
	//对接AppKeyValue
	@Value("${AppKeyValue}")
	private String AppKeyValue;

	/**
	 * listArea 获取区域集合
	 *
	 * @author azj
	 * @version 2020-04-24
	 */
	@RequiresPermissions("mobileBusiness:list")
	@GetMapping(value ="listAreaPoint")
	public AjaxJson listArea() {
		return AjaxJson.success().put("data",mobilebusinessService.listAreaPoint());
	}

	/**
	 * listArea 获取某区域业务集合
	 * areaId点位标识
	 * 是否热门isTop
	 * @author azj
	 * @version 2020-04-24
	 */
	@RequiresPermissions("mobileBusiness:list")
	@GetMapping(value ="listTypeName")
	public AjaxJson list(String areaId,String isTop,String pageNo,String pageSize) {
		return AjaxJson.success().put("data",mobilebusinessService.listTypeName(areaId,isTop,pageNo,pageSize));
	}

	/**
	 * findAreaType 获取某区域业务集合
	 *
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="findAreaType")
	public AjaxJson findAreaType() {
		return AjaxJson.success().put("data",mobileMatterService.findAreaType());
	}

	/**
	 * findAreaType 获取某区域业务集合
	 * orgrizeStatus区域标识集合
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="findAreaTypeByorgrizeStatus")
	public AjaxJson findAreaTypeByorgrizeStatus(String orgrizeStatus) {
		return AjaxJson.success().put("data",mobileMatterService.findAreaTypeByorgrizeStatus(orgrizeStatus));
	}

	/**
	 * findTypeNum 获取某区域是否有业务
	 * areaId 点位id
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="findTypeNum")
	public AjaxJson findTypeNum(String areaId) {
		return AjaxJson.success().put("data",mobilebusinessService.findTypeNum(areaId));
	}

	/**
	 * findTypeNameValue 获取业务名称
	 * areaId 点位id
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="findTypeNameValue")
	public AjaxJson findTypeNameValue(String areaId,String number) {
		return AjaxJson.success().put("data",mobilebusinessService.findTypeNameValue(areaId,number));
	}

	/**
	 * findTypeNameValuePC 获取业务名称
	 * areaId 点位id
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="findTypeNameValuePC")
	public AjaxJson findTypeNameValuePC(String areaId,String number) {
		return AjaxJson.success().put("data",mobilebusinessService.findTypeNameValuePC(areaId,number)).put("id",mobilebusinessService.findHqueueId(areaId,number));
	}

	/**
	 * findUserValueByHqueueId 获取群众办理详情
	 * id 办理id
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="findUserValueByHqueueId")
	public AjaxJson findUserValueByHqueueId(@RequestHeader("AppKey") String AppKey,String id) {
		if (AppKeyValue.equals(AppKey)) {
			return AjaxJson.success().put("data",mobilebusinessService.findUserValueByHqueueId(id));
		}else{
			return AjaxJson.error("AppKey is error!").put("code", 400);
		}
	}

	/**
	 * findTypeNameValueById 获取业务名称
	 * areaId 点位id
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="findTypeNameValueById")
	public AjaxJson findTypeNameValueById(String id) {
		return AjaxJson.success().put("data",mobilebusinessService.findTypeNameValueById(id));
	}

	/**
	 * getEvaluateListPJ 获取手机端评价分类集合
	 *
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getEvaluateListPJ")
	public Result<List<MobileEvaluateValue>> getEvaluateListPJ() {
		List<MobileEvaluateValue> list =  mobileEvaluateService.getEvaluateTree();
		List<MobileEvaluate> meList = mobileEvaluateService.getEvaluateList();
		for(MobileEvaluateValue me :list){
			me.setEvaluateList(new ArrayList<MobileEvaluate>());
			for(MobileEvaluate ev : meList){
				if(ev.getEvaluateLevel().equals(me.getEvaluateLevel())){
					me.getEvaluateList().add(ev);
				}
			}
		}
		return Result.succeed(list, "获取成功");
	}

	/**
	 * insertSmsEvaluateValue 添加短信评价
	 * MobileEvaluateHistory评价实体类
	 * @author azj
	 * @version 2020-04-24
	 */
	@PostMapping(value ="insertSmsEvaluateValue")
	public AjaxJson insertSmsEvaluateValue(@RequestBody MobileEvaluateSms me) {
		int pj = 0;
		String myd = "";
		if("VerySatisfied".equals(me.getEvaluateLevel())){
			pj = 1;
		}else if("Satisfied".equals(me.getEvaluateLevel())){
			pj = 2;
		}else if("BasicallySatisfied".equals(me.getEvaluateLevel())){
			pj = 3;
		}else if("Dissatisfied".equals(me.getEvaluateLevel())){
			pj = 4;
			myd = "不满意";
		}else if("VeryDissatisfied".equals(me.getEvaluateLevel())){
			pj = 5;
			myd = "非常不满意";
		}
		//result += me.getIds();
		//if(StringUtils.isNotBlank(me.getRemark())){
		//	result += ";备注:"+me.getRemark();
		//}
		mobilebusinessService.insertUserSMSPJ(pj,me.getRemark(),me.getId());
		mobileOtherService.updatePJById(pj+"",me.getId());
		if(pj >= 4 && StringUtils.isNotBlank(me.getRemark())){
			send.toManagerPJ(me.getId(),me.getRemark(),myd);
		}
		return AjaxJson.success("操作成功！").put("data",pj);
	}


	/**
	 * getUserPJ 获取评价详情
	 * MobileEvaluateHistory评价实体类
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="getUserPJ")
	public AjaxJson getUserPJ(String id) {
		return AjaxJson.success("操作成功！").put("data",mobileSilentService.findWXPJValue(id));
	}


	/**
	 * insertSmsEvaluateValuePC 添加短信评价
	 * MobileEvaluateHistory评价实体类
	 * @author azj
	 * @version 2020-04-24
	 */
	@PostMapping(value ="insertSmsEvaluateValuePC")
	public AjaxJson insertSmsEvaluateValuePC(@RequestBody MobileEvaluateSms me) {
		logger.error("评价方法调用-编号："+me.getNumber());
		me.setId(mobilebusinessService.findNumberId(me.getAreaId(),me.getNumber()));
		int pj;
		String result = "";
		if("VerySatisfied".equals(me.getEvaluateLevel())){
			pj = 1;
		}else if("Satisfied".equals(me.getEvaluateLevel())){
			pj = 2;
		}else if("BasicallySatisfied".equals(me.getEvaluateLevel())){
			pj = 3;
		}else if("Dissatisfied".equals(me.getEvaluateLevel())){
			pj = 4;
		}else if("VeryDissatisfied".equals(me.getEvaluateLevel())){
			pj = 5;
		}else{
			logger.error(me.getAreaId()+"-"+me.getNumber()+"评价内容缺失！result:"+me.getEvaluateLevel());
			return AjaxJson.error("评价失败！");
		}
		result += me.getIds();
		if(StringUtils.isNotBlank(me.getRemark())){
			result += ";备注:"+me.getRemark();
		}
		logger.error("评价编号："+me.getNumber()+" 满意度："+pj);
		if(mobilebusinessService.insertUserSMSPJ(pj,result,me.getId()) >= 1){
			mobileOtherService.updatePJById(pj+"",me.getId());
			mobilebusinessService.updatePJValue(me.getAreaId(),me.getNumber(),pj);
			return AjaxJson.success("操作成功！");
		}else{
			return AjaxJson.error("评价失败！");
		}


	}

	/**
	 * checkUserPJ 是否评价
	 * areaId 点位id
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="checkUserPJ")
	public AjaxJson checkUserPJ(String id) {
		if(StringUtils.isNotBlank(mobilebusinessService.checkUserPJ(id))){
			return AjaxJson.success();
		}else{
			return AjaxJson.error("已评价！");
		}
	}

	/**
	 * checkUserPJ 是否评价
	 * areaId 点位id
	 * @author azj
	 * @version 2020-04-24
	 */
	@GetMapping(value ="checkUserPJPC")
	public AjaxJson checkUserPJPC(String areaId,String number) {
		if(StringUtils.isNotBlank(mobilebusinessService.checkUserPJPC(areaId,number))){
			return AjaxJson.success();
		}else{
			return AjaxJson.error("已评价！");
		}
	}



}
